package com.project.dao.impl;

import com.project.bean.UserBean;
import com.project.dao.IUserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@Repository
public class UserDaoImpl extends BaseDao implements IUserDao {
    @Override
    public UserBean land(String userName, String password) {
        UserBean user = null;
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from t_user where t_userAccount=? and t_userPassword=?");
            ps.setObject(1,userName);
            ps.setObject(2,password);
            rs = ps.executeQuery();
            if (rs.next()){
                user = this.getUser();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }
        return user;
    }

    @Override
    public List<UserBean> findAll() {
        List<UserBean>list = new ArrayList<>();
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from t_user ");
            rs = ps.executeQuery();
            while (rs.next()){
                list.add(this.getUser());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }
        return list;
    }

    @Override
    public void del(Integer id) {
      this.setConnection();
        try {
            ps = con.prepareStatement("delete from t_user where pk_userId=?");
            ps.setObject(1,id);
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }
    }

    @Override
    public UserBean findById(Integer id) {
        this.setConnection();
        UserBean user = null;
        try {
            ps = con.prepareStatement("select * from t_user where pk_userId=?");
            ps.setObject(1,id);
            rs = ps.executeQuery();
            if (rs.next()){
                user=this.getUser();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }
        return user;
    }

    @Override
    public void updateGrade(Integer id, String grade) {
           this.setConnection();
        try {
            ps = con.prepareStatement("update t_user set t_userGrade=? where pk_userId=?");
            ps.setObject(1,grade);
            ps.setObject(2,id);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }
    }

    @Override
    public void updatePassword(Integer id, String password) {
        this.setConnection();
        try {
            ps = con.prepareStatement("update t_user set t_userPassword=? where pk_userId=?");
            ps.setObject(1,password);
            ps.setObject(2,id);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }
    }

    @Override
    public void add(UserBean user) {
       this.setConnection();
        try {
            ps =con.prepareStatement("insert into t_user(t_userAccount,t_userPassword,t_userName,t_userGrade) values (?,?,?,?)");
            ps.setObject(1,user.getUserName());
            ps.setObject(2,user.getPassword());
            ps.setObject(3,user.getName());
            ps.setObject(4,user.getGrade());
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            this.closeConnection();
        }
    }

    @Override
    public boolean checkName(String name) {
        this.setConnection();
        UserBean user = null;
        try {
            ps = con.prepareStatement("select * from t_user where t_userAccount=?");
            ps.setObject(1, name);
            rs = ps.executeQuery();
            if (rs.next()) {
                user = this.getUser();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeConnection();
        }
        if (user == null) {
            return true;
        } else {
            return false;

        }
    }
    public UserBean getUser(){
        UserBean user = new UserBean();
        try {
            user.setUserName(rs.getString("t_userAccount"));
            user.setId(rs.getInt("pk_userId"));
            user.setPassword(rs.getString("t_userPassword"));
            user.setName(rs.getString("t_userName"));
            user.setGrade(rs.getString("t_userGrade"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }

    public static void main(String[] args) {
//        IUserDao dao = new UserDaoImpl();
//        System.out.println(dao.land("张三","123"));
//        System.out.println(dao.findAll());
//        System.out.println(dao.findById(2));
//        System.out.println(dao.checkName("张三1"));
//        dao.add(new UserBean("wuwu","li","班主任"));
//        dao.del(6);
//        dao.updateGrade(1,"超级管理员");
//        dao.updatePassword(1,"111");
    }
}


